//
//  NetEngine.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/3/30.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class NetEngine: NSObject {
   
    /// 获取主界面数据
    /// - Parameters:
    ///   - ownid: 房间号
    ///   - success: 成功后回调
    ///   - fail: 失败后回调
    class func respose_yhthost(ownid:String,success:(([String:AnyObject])->())? = nil,fail:(()->())? = nil) {
        HUD.show()
        let params:[String:Any] = ["ownid":ownid]
        NetworkTool.shared.postRequest(urlString: mt_url_yhthostmeeting, params: params, success: { (result) in
            HUD.dismiss()
            let code = result["code"] as? Int
            if code != nil {
                switch code! {
                case 0:
                    if success != nil {
                        success!(result)
                    }
                default:
                    let msg = result["msg"] as? String ?? "code!=0"
                    MTToast.show(msg: msg)
                    if fail != nil {
                        fail!()
                    }
                }
            }
            
        }) { (error) in
            HUD.dismiss()
            showNETFail()
        }
    }
    
    
    /// 获取会议号
    /// - Parameters:
    ///   - machineId: 设备ID
    ///   - success: 成功后回调
    ///   - fail: 失败后回调
    class func respose_getMeetingNum(ownidKey:String,success:(([String:AnyObject])->())? = nil,fail:(()->())? = nil) {
        let params:[String:Any] = ["wifiId":ownidKey]
        NetworkTool.shared.postRequest(urlString: mt_url_getMeetingNum, params: params, success: { (result) in
            let code = result["code"] as? Int
            if code != nil {
                switch code! {
                case 0:
                    if success != nil {
                        success!(result)
                    }
                default:
                    let msg = result["msg"] as? String ?? "code=0"
                    MTToast.show(msg: msg)
                    if fail != nil {
                        fail!()
                    }
                }
            }
            
        }) { (error) in
            HUD.dismiss()
            showNETFail()
        }
    }
    
    /// 获取会议号
    /// - Parameters:
    ///   - memid: 设备ID
    ///   - checkCode: 校研码
    ///   - success: 成功后回调
    ///   - fail: 失败后回调
    class func respose_userInfo(memid:String,checkCode:String,success:(([String:AnyObject])->())? = nil,fail:(()->())? = nil) {
        let params:[String:Any] = ["memid":memid,"checkCode":checkCode]
        NetworkTool.shared.postRequest(urlString: mt_url_userInfo, params: params, success: { (result) in
            let code = result["code"] as? Int
            if code != nil {
                switch code! {
                case 0:
                    if success != nil {
                        success!(result)
                    }
                default:
                    if fail != nil {
                        fail!()
                    }
                }
            }
            
        }) { (error) in
            showNETFail()
        }
    }
    
    /// 修改名字
    /// - Parameters:
    ///   - name: 需要设置的名字
    ///   - ownId: 房间号
    ///   - success: 成功后回调
    class func reponseName(_ name:String,id ownId:String,success:(()->())? = nil){
        let paramets = ["ownid":ownId, "display_name":name]
        NetworkTool.requestData(type: .post, URLString: mt_url_changeappname, parameters: paramets, finishCallback: { (result) in
            UserInfoManager.shared.disname = name
            if success != nil {
                success!()
            }
        }) { (error) in
            MTToast.show(msg: "名称保存失败".localized())
        }
    }
    

    class func getJoinActivityId(activityId:String,success:(([String:AnyObject])->())? = nil) {
        let paramets = ["id":activityId]
        NetworkTool.requestData(type: .post, URLString: mt_url_connectService, parameters: paramets,finishCallback: { (result) in
            let code = result["code"] as? Int
            if code != nil {
                switch code! {
                case 0:
                    if success != nil {
                        success!(result as! [String : AnyObject])
                    }
                default:
                    let msg = result["msg"] as? String ?? "code!=0"
                    MTToast.show(msg: msg)
                }
            }
            
         }) { (error) in
             showNETFail()
         }
     }
     
     class func getJoinId(ownidKey:String,meetingid:String,success:(([String:AnyObject])->())? = nil){
         let params : [String : String]? = ["wifiId":ownidKey,"id":meetingid]
         
         NetworkTool.requestData(type:  .post, URLString:mt_url_terminalConference,parameters: params!, finishCallback: { (result) in
            HUD.dismiss()
            let code = result["code"] as? Int
            if code != nil {
                switch code! {
                case 0:
                    if success != nil {
                        success!(result as! [String : AnyObject])
                    }
                default:
                    let msg = result["msg"] as? String ?? "code!=0"
                    MTToast.show(msg: msg)
                }
            }
             
        }) { (error) in
            HUD.dismiss()
            showNETFail()
        }
    }
     
     
     // 一体机：1001，安卓：1002，iOS：1003，window：1004，mac：1005
     class func getHostInfo(wifiId:String,uuid:String,success:(([String:AnyObject])->())? = nil,fail:(()->())? = nil){
         
         let id = uuid
        let params:[String:String]? = ["machineId":id, "wifiId":wifiId,"networkId":"","type":"3","version":AppInfo.majorVersion()];
       
         NetworkTool.requestData(type: .post, URLString: mt_url_terminalVideo, parameters: params!, finishCallback: { (result) in
            let mode = SJHostModel.deserialize(from: result as? [String:Any])
             switch mode?.code {
             case 0:
                if success != nil {
                    success!(result as! [String:AnyObject])
                }
             default:
                if fail != nil {
                    fail!()
                }
             }
            
            
         }) { (error) in
             showNETFail()
         }
     }
    
    
    
    
    class func reponse_verify(phone:Int,success:(([String:AnyObject])->())? = nil,fail:(()->())? = nil){
        let paramets = ["phone":"\(phone)"]
        NetworkTool.requestData(type: .post, URLString: mt_url_verification, parameters: paramets, finishCallback: { (result) in
            let model = VerrifyModel.deserialize(from: result as? [String:Any])
            switch model?.code {
            case 0:
                if success != nil {
                    success!(result as! [String:AnyObject])
                }
            default:
                if fail != nil {
                    fail!()
                }
                show_msg((model?.response?.Message)!)
            }
            
        }) { (error) in
            showNETFail()
        }
    }
    
    class func reponse_checkdtm(phone:Int,nextzym:Int,success:((SJMemMode)->())? = nil,fail:(()->())? = nil){
        let paramets = ["phone":"\(phone)","nextzym":"\(nextzym)"]
        NetworkTool.requestData(type: .post, URLString: mt_url_checkdtm, parameters: paramets, finishCallback: { (result) in
            
            let model = SJMemMode.deserialize(from: result as? [String:Any])
            switch model?.code {
            case 0:
                if success != nil {
                    success!(model!)
                }
            default:
                if fail != nil {
                    fail!()
                }
                let w = model?.msg==nil ? "验证码错误".localized():model?.msg
                show_msg(w!)
            }
            
        }) { (error) in
            showNETFail()
        }
    }
    
    class func reponse_userPwdlogin(username:String,password:String,success:((SJMemMode)->())? = nil,fail:(()->())? = nil){
        let paramets = ["username":"\(username)","password":"\(password)"]
        NetworkTool.requestData(type: .post, URLString: mt_url_userPwdlogin, parameters: paramets, finishCallback: { (result) in
            let model = SJMemMode.deserialize(from: result as? [String : Any])
            switch model?.code {
            case 0:
                if success != nil {
                    success!(model!)
                }
            default:
                if fail != nil {
                    fail!()
                }
                let w = model?.msg ?? "登录失败".localized()
                show_msg(w)
            }
        }) { (error) in
            HUD.dismiss()
            showNETFail()
        }
    }
    
    class func reponse_wxLogin(unionid:String,openid:String,image_path:String,name:String,success:((SJMemMode)->())? = nil,fail:(()->())? = nil){
        let paramets = ["unionid":"\(unionid)","openid":"\(openid)","image_path":"\(image_path)","name":"\(name)"]
        NetworkTool.requestData(type: .post, URLString: mt_url_wxLogin, parameters: paramets, finishCallback: { (result) in
            let model = SJMemMode.deserialize(from: result as? [String:Any])
            switch model?.code {
            case 0:
                if success != nil {
                    success!(model!)
                }
            default:
                let w = model?.msg ?? "登录失败".localized()
                show_msg(w)
                if fail != nil {
                    fail!()
                }
            }
        }) { (error) in
            showNETFail()
        }
    }

    // 一体机：1001，安卓：1002，iOS：1003，window：1004，mac：1005
    class func reponse_announce(ownid:String,version:String,device:String,success:((AnnounceMode)->())? = nil,fail:(()->())? = nil){
        let paramets = ["ownid":"\(ownid)","version":"\(version)","device":"\(device)"]
        NetworkTool.requestData(type: .post, URLString: mt_url_announce, parameters: paramets, finishCallback: { (result) in
            let mode = AnnounceMode.deserialize(from: result as? [String:Any])
            switch mode?.code {
            case 0:
                if success != nil {
                    success!(mode!)
                }
            default:
                let w = mode?.msg ?? "登录失败".localized()
                show_msg(w)
            }
        }) { (error) in
            if fail != nil {
                fail!()
            }
            showNETFail()
        }
    }
    
    
    class func reponse_wxQrLogin(out:String,success:((QRMemMode)->())? = nil,fail:(()->())? = nil){
        let paramets = ["out":out]
        NetworkTool.requestData(type: .get, URLString: mt_url_wxQrLogin, parameters: paramets, finishCallback: { (result) in
            let model = QRMemMode.deserialize(from: result as? [String:Any])
            switch model?.code {
            case 0:
                if success != nil {
                    success!(model!)
                }
            default:
                if fail != nil {
                    fail!()
                }
            }
        }) { (error) in
            showNETFail()
        }
    }
    
    
    
}
